package com.xzc.lease.common.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
//开启@ConfigurationProperties注解。‌ 通过使用@EnableConfigurationProperties注解，
//  可以启用对特定配置类的支持，使得配置文件中的参数能够被转化为Java对象，并在Spring容器中进行管理‌
@EnableConfigurationProperties(AliyunSMSProperties.class)
//该注解表达的含义是只有当 aliyun.sms.endpoint 属性存在时，该配置类才会生效，web-admin模块不能获取该值会导致项目不能正常启动，因此引入该注解
@ConditionalOnProperty(name = "aliyun.sms.endpoint")
public class AliyunSMSConfiguration {

    @Autowired
    private AliyunSMSProperties properties;

    @Bean
    public Client createClient() {
        Config config = new Config();
        config.setAccessKeyId(properties.getAccessKeyId());
        config.setAccessKeySecret(properties.getAccessKeySecret());
        config.setEndpoint(properties.getEndpoint());
        try {
            return new Client(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}